package com.jia.train.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.jia.train.global.utils.SnowIdUtil;
import com.jia.train.member.entity.Ticket;
import com.jia.train.member.mapper.TicketMapper;
import com.jia.train.member.request.TicketSaveRequest;
import com.jia.train.member.response.TicketSaveResponse;
import com.jia.train.member.service.TicketService;
import io.seata.core.context.RootContext;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.util.Date;
import java.util.List;

@Service
public class TicketServiceImpl implements TicketService {

    public static final Logger logger = LoggerFactory.getLogger(TicketServiceImpl.class);

    @Resource
    private TicketMapper ticketMapper;

    @Resource
    private DataSource dataSource;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<TicketSaveResponse> batchInsert(List<TicketSaveRequest> list) {
        logger.info("member seata 全局事务ID：{}", RootContext.getXID());
        logger.info("当前数据源类型：{}", dataSource.getClass().getName());
        List<Ticket> ticketList = BeanUtil.copyToList(list, Ticket.class);
        for (Ticket ticket : ticketList) {
            ticket.setId(SnowIdUtil.getLongSnowNextId());
            ticket.setCreateTime(new Date());
        }
        ticketMapper.batchInsert(ticketList);
        return BeanUtil.copyToList(ticketList, TicketSaveResponse.class);
    }

}
